package org.example.test5;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentInfoInput {
    public static void main(String[] args) {
        /*5.以字符串的形式从键盘录入5个学生信息,格式:"张三,23,95",
将该字符串切割出有效数据,封装为Student学生对象(姓名,年龄,分数),如果是数据一样则不能进行存储*/
        Scanner scanner = new Scanner(System.in);
        List<Student> students = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            System.out.print("请输入学生信息（格式：姓名,年龄,分数）：");
            String input = scanner.nextLine();
            String[] parts = input.split(",");
            
            if (parts.length == 3) {
                String name = parts[0];
                int age = Integer.parseInt(parts[1]);
                int score = Integer.parseInt(parts[2]);
                Student student = new Student(name, age, score);

                if (!students.contains(student)) {
                    students.add(student);
                } else {
                    System.out.println("相同信息的学生已存在，跳过该记录。");
                }
            } else {
                System.out.println("输入格式不正确，跳过该记录。");
            }
        }

        System.out.println("学生信息列表：");
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

class Student {
    private String name;
    private int age;
    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Student student = (Student) obj;
        return age == student.age &&
                score == student.score &&
                name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, score);
    }

    @Override
    public String toString() {
        return "学生信息{" +
                "姓名='" + name + '\'' +
                ", 年龄=" + age +
                ", 分数=" + score +
                '}';
    }
}
